package com.splant.android.smart.UILevel;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.TextView;

import com.splant.android.R;
import com.splant.android.smart.SystemDefinedLevel.GenericControls.Dialog.LoadingDialog;
import com.splant.android.smart.SystemDefinedLevel.Utils.System_Util;
import com.splant.android.smart.UILevel.Community.Garden_Base_Activity;

public class System_Web_Activity extends Garden_Base_Activity implements View.OnClickListener {

    private TextView tv_title;
    private TextView tv_close;
    private WebView wv_webView;
    private Button btn_goBack;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.system_web_activity);

        System_Util.configLanguage(this);

        btn_goBack = (Button) findViewById(R.id.btn_goBack);
        tv_title = (TextView) findViewById(R.id.tv_title);
        tv_close = (TextView) findViewById(R.id.tv_close);
        wv_webView = (WebView) findViewById(R.id.wv_webView);

        wv_webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
        WebSettings settings = wv_webView.getSettings();
        settings.setPluginState(WebSettings.PluginState.ON);
        settings.setJavaScriptEnabled(true);
        settings.setDomStorageEnabled(true);

        btn_goBack.setOnClickListener(this);
        tv_close.setOnClickListener(this);

        try
        {
            showWebPage();
        } catch (Exception e) {

        }
    }

    private String homeUrl="";
    private void showWebPage() {
      String  url = getIntent().getStringExtra("url");
        String title = getIntent().getStringExtra("title");

        if (url.equals("") == false) {
            tv_title.setText(title);

            loadingDialog = LoadingDialog.createDialog(System_Web_Activity.this);
            loadingDialog.show();

            wv_webView.setWebViewClient(webViewClient);

            wv_webView.loadUrl(url);


        }

    }

    private LoadingDialog loadingDialog;
    private WebChromeClient webChromeClient = new WebChromeClient() {
        @Override
        public void onProgressChanged(WebView view, int progress) {

            if (progress == 100) {
                if (loadingDialog != null)
                    loadingDialog.dismiss();
            }
        }
    };

    private WebViewClient webViewClient = new WebViewClient() {
        @Override
        public void onPageFinished(WebView view, String url) {

            if (loadingDialog != null)
                loadingDialog.dismiss();

            if (homeUrl.equals(""))
                homeUrl=url;
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (url.startsWith("intent") || url.startsWith("youku")) {
                return true;
            } else {
                return super.shouldOverrideUrlLoading(view, url);
            }
        }
    };

    private boolean isOnPause = false;

    @Override
    protected void onPause() {
        super.onPause();
        try {
            if (wv_webView != null) {
                wv_webView.getClass().getMethod("onPause").invoke(wv_webView, (Object[]) null);
                isOnPause = true;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.btn_goBack:

                goBack();

                break;
            case R.id.tv_close:
                finish();
                break;

        }
    }
    private void  goBack()
    {
        String curUrl=wv_webView.getUrl();
        if (homeUrl.equals(curUrl))
        {
            finish();
        }
        else
            wv_webView.goBack();
    }

    /**
     * 当Activity执行onResume()时让WebView执行resume
     */
    @Override
    protected void onResume() {

        super.onResume();
        try {
            if (isOnPause) {
                if (wv_webView != null) {
                    wv_webView.getClass().getMethod("onResume").invoke(wv_webView, (Object[]) null);
                }
                isOnPause = false;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 该处的处理尤为重要:
     * 应该在内置缩放控件消失以后,再执行mWebView.destroy()
     * 否则报错WindowLeaked
     */

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message message) {
            wv_webView.destroy();
            wv_webView = null;
        }
    };

    @Override
    protected void onDestroy() {

        super.onDestroy();

        if (wv_webView != null) {

            wv_webView.getSettings().setBuiltInZoomControls(true);
            wv_webView.setVisibility(View.GONE);
            long delayTime = ViewConfiguration.getZoomControlsTimeout();
            handler.sendEmptyMessageDelayed(0, delayTime);

        }
        isOnPause = false;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK) {
            goBack();
        }
        return false;
    }
}
